"use strict";

/**
 * Created by zt on 2018/4/15 0015.
 */

/**
 *请求后台服务器的内容
 **/
define(['axios','Qs'],function (axios,Qs) {
    /**创建axios实例*/
        //var token = getCookie("token");
    var service = axios.create({
            baseURL: '/api',
            // api的base_url
            timeout: 30000 // 请求超时时间
            //headers: {'Content-Type': 'application/json; charset=UTF-8'},
            //responseType:'json'

        });
    /**添加请求拦截器*/

    service.interceptors.request.use(function (config) {
        var currentMethod = config.method.toUpperCase();

        if (currentMethod == 'POST') {
            config.headers['Content-Type'] = "application/x-www-form-urlencoded";
            config.data = Qs.stringify(config['data']);
        }

        config.headers['X-Requested-With'] = "XMLHttpRequest";
        return config;
    }, function (error) {
        console.error('请求异常', error);
        return Promise.reject(error);
    }); // 添加响应拦截器

    service.interceptors.response.use(function (response) {
        console.log(response.data.error);
        if (response.data.error == 0) {
            //ret 0成功  1失败
            return response.data;
        } else if (response.data.error ==401) {
            //未登录是跳转到登录页
             setTimeout(function () {
                 window.location.href = response.data.data.url;
             },3000);
            return response.data;
        } else {
            return response.data;
        }
    }, function (error) {
        return Promise.reject(error);
    });
    var fetch = service;
    window.Fetch = fetch;
})


